﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Utility;
using Frame;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace Log
{
    public partial class LogShowWindow : Plugin
    {
        Color infoColor;
        Color warningColor;
        Color errorColor;
        Color fatalColor;
        ContextMenuStrip menu;

        bool isHideAPI = true;
        Dictionary<int, Color> colorDic = new Dictionary<int, Color>();

        public LogShowWindow(FrameApplication app) : base(app)
        {
            InitializeComponent();
            infoColor = Color.Green;
            warningColor = Color.BlueViolet;
            errorColor = Color.Red;
            fatalColor = Color.IndianRed;
            menu = new ContextMenuStrip();
            menu.Name = "LogWindowMenu";
            menu.Items.Add("清空", null, LogClear);
            lvLog.ContextMenuStrip = menu;

            this.FloatSize = this.Size;
            this.m_InitShow = true;
            this.toolBarIcon = Properties.Resources.LogShowWindow;
            this.OnMenu = "OTHER";

            app.Log.LogEvent += LogShow;

            lvLog.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            lvLog.Items.Clear();
            for (int i = 0; i < m_app.Log.LogList.Count; i++)
            {
                if (isHideAPI && m_app.Log.LogList[i].level != LogLevel.API)
                {
                    AddItem(m_app.Log.LogList[i]);
                }
            }
        }

        public override void InitUI()
        {
            
        }

        void AddItem(LogInfo log)
        {
            if (lvLog.InvokeRequired)
            {
                lvLog.Invoke(new Action<LogInfo>(AddItem), new object[] { log });
            }
            else
            {
                ListViewItem i = lvLog.Items.Add("");
                i.SubItems.Add(log.text);
                i.ForeColor = log.GetColor();
                colorDic.Add(lvLog.Items.Count - 1, log.GetColor());
                lvLog.Items[lvLog.Items.Count - 1].Selected = true;//选中最后一行 
                lvLog.Items[lvLog.Items.Count - 1].EnsureVisible();//显示内容自动滚动到最后一行
            }


        }

        private void ShowAll(object sender, EventArgs e)
        {
            lvLog.Items.Clear();
            colorDic.Clear();
            for (int i = 0; i < m_app.Log.LogList.Count; i++)
            {
                AddItem(m_app.Log.LogList[i]);
            }
            isHideAPI = false;
        }
        private void HideAPI(object sender, EventArgs e)
        {            
            lvLog.Items.Clear();
            colorDic.Clear();
            for (int i = 0; i < m_app.Log.LogList.Count; i++)
            {
                if (m_app.Log.LogList[i].level != LogLevel.API)
                {
                    AddItem(m_app.Log.LogList[i]);
                }
            }
            isHideAPI = true;
        }

        private void LogClear(object sender, EventArgs e)
        {
            lvLog.Items.Clear();
            colorDic.Clear();
            m_app.Log.LogList.Clear();
        }

        public void LogShow()
        {
            if (isHideAPI && m_app.Log.LogList.Last().level == LogLevel.API)
            {
                return;
            }
            AddItem(m_app.Log.LogList.Last());
        }

        private void lvLog_SizeChanged(object sender, EventArgs e)
        {
            lvLog.Columns[0].Width = 0;
            lvLog.Columns[1].Width = this.Width;
            lvLog.Refresh();
            //lvLog.OnMouseWheel();
        }


        private void LogShowWindow_SizeChanged(object sender, EventArgs e)
        {
            this.Refresh();            
        }
    }
}
